package com.gitee.qdbp.general.common.biz.oplog.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.result.BatchResult;
import com.gitee.qdbp.able.result.IBatchResult;
import com.gitee.qdbp.able.result.ResultCode;
import com.gitee.qdbp.base.controlling.OperateTracesBean;
import com.gitee.qdbp.general.common.api.oplog.service.IOperateTracesExecutor;
import com.gitee.qdbp.general.common.biz.oplog.basic.OperateTracesBasic;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 操作记录明细业务处理类
 *
 * @author zhh
 * @version 170522
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class OperateTracesExecutor implements IOperateTracesExecutor {

    /** 日志对象 **/
    private static final Logger log = LoggerFactory.getLogger(OperateTracesExecutor.class);

    /** 操作记录明细DAO **/
    @Autowired
    private OperateTracesBasic operateTracesBasic;

    @Override
    public void create(OperateTracesBean model) throws ServiceException {
        String msg = "Failed to create OperateTraces. ";

        if (VerifyTools.isBlank(model)) {
            log.trace(msg + "params is null: model");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 参数验证
        if (VerifyTools.isBlank(model.getOperateType())) {
            log.trace(msg + "params is null: OperateType");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(model.getRecordId())) {
            log.trace(msg + "params is null: RecordId");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 向sys_operate_item表插入记录
        operateTracesBasic.create(model);
    }

    @Override
    public IBatchResult create(List<OperateTracesBean> models) throws ServiceException {
        String msg = "Failed to create OperateTraces. ";

        if (VerifyTools.isBlank(models)) {
            log.trace(msg + "params is null: models");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        BatchResult result = new BatchResult();
        result.setTotal(models.size());
        int i = 0;
        List<OperateTracesBean> filtered = new ArrayList<>();
        for (OperateTracesBean model : models) {
            i++; // 行号从1开始
            // 参数验证
            if (VerifyTools.isBlank(model.getOperateType())) {
                result.addFailed(i, "OperateType", ResultCode.PARAMETER_IS_REQUIRED);
            } else if (VerifyTools.isBlank(model.getRecordId())) {
                result.addFailed(i, "RecordId", ResultCode.PARAMETER_IS_REQUIRED);
            } else {
                filtered.add(model);
            }
        }

        if (!filtered.isEmpty()) {
            // 向sys_operate_item表插入记录
            operateTracesBasic.create(filtered);
        }

        return result;
    }

    @Override
    public void delete(String id) throws ServiceException {
        String msg = "Failed to delete OperateTraces. ";

        if (VerifyTools.isBlank(id)) {
            log.error(msg + "params is null: id");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 删除sys_operate_item表的记录
        operateTracesBasic.deleteByIds(Arrays.asList(id), false);
    }

    @Override
    public void delete(List<String> ids) throws ServiceException {
        String msg = "Failed to delete OperateTraces. ";

        if (VerifyTools.isBlank(ids)) {
            log.error(msg + "params is null: ids");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 删除sys_operate_item表的记录
        operateTracesBasic.deleteByIds(ids, false);
    }

}
